package com.woniu.crowd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.crowd.common.CrowdException;
import com.woniu.crowd.infra.model.CrowdfundingProducts;
import com.woniu.crowd.infra.model.SelectLog;
import com.woniu.crowd.infra.model.SelectProduct;
import com.woniu.crowd.infra.mapper.SelectProductMapper;
import com.woniu.crowd.infra.model.es.CrowdEsModel;
import com.woniu.crowd.infra.model.es.SelectEsModel;
import com.woniu.crowd.service.SelectLogService;
import com.woniu.crowd.service.SelectProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.crowd.service.dto.CrowdEsModelDto;
import com.woniu.crowd.service.dto.PageCrowdEsDto;
import com.woniu.crowd.service.dto.PageSelectEsDto;
import com.woniu.crowd.service.dto.SelectEsModelDto;
import com.woniu.crowd.service.form.SearchSelectForm;
import com.woniu.crowd.service.form.UpSelectForm;
import com.woniu.exception.LXLGException;
import com.woniu.product.client.ProductQueryClient;
import com.woniu.product.client.result.SpuResultInfo;
import com.woniu.result.Result;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 甄选商品表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-26
 */
@Service
public class SelectProductServiceImpl extends ServiceImpl<SelectProductMapper, SelectProduct> implements SelectProductService {
@Resource
private ProductQueryClient productQueryClient;
@Resource
private ElasticsearchOperations eo;
@Resource
private SelectLogService selectLogService;
    @Override
    public Result upSelect(UpSelectForm upSelectForm) {
        SpuResultInfo spu = productQueryClient.getSpuInfoBySpuId(upSelectForm.getProductId(),-1).getData();
        if (spu== null) throw new LXLGException(CrowdException.CROWD_PRODUCT_NON_EXISTENT);
        if (spu.getSpuStatus().equals("ON_SALE")) throw new LXLGException(CrowdException.CROWD_ON_THE_SHELF);

        SelectProduct selectProduct = BeanUtil.copyProperties(upSelectForm, SelectProduct.class);
        selectProduct.setSelectStatus("CREATED");
        save(selectProduct);

        SelectEsModel selectEsModel = BeanUtil.copyProperties(spu, SelectEsModel.class);
        selectEsModel.setSelectProductId(selectProduct.getSelectProductId());
        selectEsModel.setSelectStatus("CREATED");
        selectEsModel.setSelectUserCount(selectProduct.getSelectUserCount());
        selectEsModel.setStartTime(selectProduct.getStartTime());
        selectEsModel.setEndTime(selectProduct.getEndTime());
        eo.save(selectEsModel);
        SelectLog selectLog = new SelectLog();
        selectLog.setSelectProductId(selectProduct.getSelectProductId());
        selectLog.setStatus(1);
        selectLogService.save(selectLog);


        return Result.getInstance().setMsg("上架成功").setCode(200);

    }

    @Override
    public Result searchCrowd(SearchSelectForm searchSelectForm) {
        NativeSearchQueryBuilder nsqb = new NativeSearchQueryBuilder();
        BoolQueryBuilder bqb1 = new BoolQueryBuilder();
        BoolQueryBuilder bqb2 = new BoolQueryBuilder();
        //关键字
        if (searchSelectForm.getKeyword() != null && !"".equals(searchSelectForm.getKeyword().trim()) && searchSelectForm.getKeyword().length() > 0) {

            bqb2.should().add(new MatchQueryBuilder("spuName", searchSelectForm.getKeyword()));
            bqb2.should().add(new MatchQueryBuilder("categoryName", searchSelectForm.getKeyword()));
            bqb2.should().add(new MatchQueryBuilder("introduction", searchSelectForm.getKeyword()));
            bqb1.must().add(bqb2);
        }
        //价格区间
        RangeQueryBuilder rqb = null;
        if (searchSelectForm.getStartPrice() != null && searchSelectForm.getEndPrice() != null) {

            rqb = new RangeQueryBuilder("minPrice");
            rqb.gte(searchSelectForm.getStartPrice());
            rqb.lte(searchSelectForm.getEndPrice());
            bqb1.must().add(rqb);
        }
        RangeQueryBuilder rqb1 = new RangeQueryBuilder("minPrice");
        if (searchSelectForm.getStartPrice() != null) {
            rqb1.gte(searchSelectForm.getStartPrice());
            bqb1.must().add(rqb1);
        }
        if (searchSelectForm.getEndPrice() != null) {
            rqb1.lte(searchSelectForm.getEndPrice());
            bqb1.must().add(rqb1);
        }

        nsqb.withQuery(bqb1);
        //分页
        nsqb.withPageable(PageRequest.of(searchSelectForm.getIndexPage() - 1, searchSelectForm.getPageSize()));
        NativeSearchQuery nsq = nsqb.build();
        SearchHits<SelectEsModel> sh = eo.search(nsq, SelectEsModel.class);
        List<SelectEsModelDto> bds = sh.getSearchHits().stream().map(searchhit -> {
            SelectEsModelDto selectEsModelDto = BeanUtil.copyProperties(searchhit.getContent(), SelectEsModelDto.class);
            return selectEsModelDto;
        }).collect(Collectors.toList());
        PageSelectEsDto esDto = new PageSelectEsDto();
        esDto.setTotal(sh.getTotalHits());
        esDto.setSpuEsList(bds);
        return Result.getInstance(PageSelectEsDto.class).setData(esDto).setCode(200).setMsg("搜索成功");
    
    }
}
